//Date.cc
#include "Date.h"
#include <iostream>
using namespace std;
namespace My_code
{
    int leap_year(int n)
    {
        if(n%4 == 0 && n%100 != 0)
        return 1;
        return 0;
    }
    Date Date::default_day(20,may,2017);
    void Date::set_default(int d,Month m,int y)
    {
        Date::default_day = Date(d,m,y);
    }
    Date::Date(int dd,int mm,int yy)
    {
        mm = (Month)mm;
        if(dd == 0)dd = default_day.day();
        if(mm == 0)mm = default_day.month();
        if(yy == 0)yy = default_day.year();
        int maxday;
        switch (mm)
        {
        case jan:
        case may:
        case mar:
        case jul:
        case aug:
        case oct:
        case dec:
            maxday = 31;
            break;
        case feb:
            maxday = 28 + leap_year(yy);
            break;

        case apr:
        case jun:
        case sep:
        case nov:
            maxday = 30;
            break;
        default:
            cout<<"初始化失败"<<endl;
            break;
        }
        if(dd < 1 || dd > maxday)cout<<"初始化失败"<<endl;
        d = dd;
        y = yy;
        m = mm;
        }
    Date::Date()
    {
        d = default_day.d;
        m = default_day.m;
        y = default_day.y;
     }
    Date& Date::Add_year(int n)
    {
        if(d == 29 && m ==2 && leap_year(y + n) != 0)
        {
            d = 1;
            m = 3;
        }
        y += n;
        return *this;
    }
    Date& Date::Add_month(int n)
    {
        if(n == 0)return *this;
        if(n > 0 )
        {
            int temp_y = n/12;
            int temp_m = m + n % 12;
            // cout<<"temp_m"<<temp_m<<endl;
            if(temp_m >12)
            {
                temp_y++;
                m = (temp_m - 12);
                y += temp_y;
            }
            else{
                m = Month(temp_m);
            }
            //cout<<"m:"<<m<<endl;
        }
        if(n < 0)
        {
            cout<<"参数有误"<<endl;
        }
        return *this;
    }
    Date& Date::Add_day(int n)
    {
        bool flag = true;
        int maxday;
        if(n == 0)return *this;
        while(n > 0 && flag)
        {
            switch(m)
            {
            case jan:
            case may:
            case mar:
            case jul:
            case aug:
            case oct:
            case dec:
                maxday = 31;
                break;
            case feb:
                maxday = 28 + leap_year(y);
                break;
            case apr:
            case jun:
            case sep:
            case nov:maxday = 30;
                break;
            default:
                cout<<"Add_day失败"<<endl;
            break;
            }
            if(d + n > maxday)
                {
                    Add_month(1);
                    if(d + n < maxday * 2) d = d + n - maxday;
                    n = n - maxday;
                }
                else flag = false;
                cout<<"maxday:"<<maxday<<"m:"<<m<<" d:"<<d<<" n:"<<n<<endl;
        }
        return *this;
    }
    Date::Date(const Date& t)
    {
        d = t.d;
        m = t.m;
        y = t.y;
    }
    Date& Date::operator=(const Date& t)
    {
        if(this == &t)return *this;
        d = t.d;
        m = t.m;
        y = t.y;
        return *this;
    }

    void Date::Show()
    {
        cout<<(Month)m<<"/"<<d<<"/"<<y<<endl;
    }
    Date::Date(int n)
    {
        d = n;
    }
    Time::Time(int hh,int mm,int ss)
    {
        m_h = hh;
        m_m = mm;
        m_s = ss;
    }
    Time::Time(){}
    Time::~Time(){}
    void Time::SetTime_h(int h)
    {
        m_h = h;
    }
    int Time::GetTime_h()
    {
        return m_h;
    }
    void Time::SetTime_m(int m)
    {
        m_m = m;
    }
    int Time::GetTime_m()
    {
        return m_m;
    }
    void Time::SetTime_s(int s)
    {
        m_s = s;
    }
    int Time::GetTime_s()
    {
        return m_s;
    }
    void Time::Show()
    {
        std::cout<<m_h<<":"<<m_m<<":"<<m_s<<std::endl;
    }
}
